package com.hqbzl.c1.aqs;

import java.util.concurrent.*;

public class TestCompletionService {
    public static void main(String[] args) throws Exception {
        BlockingQueue q = new LinkedBlockingQueue();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 6, 3, TimeUnit.SECONDS, q);
        CompletionService<Integer> cs = new ExecutorCompletionService<>(executor);

        final Future<Integer> submit1 = cs.submit(() -> {
            Thread.sleep(2000);
            return 1;
        });
        final Future<Integer> submit2 = cs.submit(() -> {
            Thread.sleep(2000);
            return 1;
        });

        for (int i = 0; i < 3; i++) {
            Integer f = cs.take().get();
            System.out.println(f);
        }
    }
}
